#!/bin/bash

## Copyright (c) 2013, PoiXson, Mattsoft
## <http://poixson.com/>
## <http://mattsoft.net/>
## 
## Permission to use, copy, modify, and/or distribute this software for any
## purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
## 
## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
## WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
## MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
## ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
## WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
## ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
## OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.


if [ -z "$DEBUG" ]; then
	source $(dirname $0)/_common.sh
fi
title "Running daily backup..."

# send commands to bukkit server
if [ -f /bukkit/sendcommand.sh ]; then
	sh /bukkit/sendcommand.sh "say Backup is running.."
	sh /bukkit/sendcommand.sh "save-all"
fi
# file doesn't exist
if [ ! -f ${PXN_SCRIPTS}/backups.txt ]; then
	echo "File ${PXN_SCRIPTS}/backups.txt not found!"
	exit
fi
# log backups
mkdir "${PXN_BACKUPS}/logs" 2> /dev/null
log_file="${PXN_BACKUPS}/logs/"$(date "+%Y-%m-%d")".txt"
touch "${log_file}"
# run backups
while read line; do
	if [ -n "$line" ]; then
		sh ${PXN_SCRIPTS}/backup.sh $line | tee -a "${log_file}"
	fi
done < "${PXN_SCRIPTS}/backups.txt"

title "Finished daily backup!"

